﻿using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SXFramWork.Core;
using SXFramWork.Core.Quartz;
using SXFramWork.Model;
using SXFramWork.Service;

namespace SXFramWork.API.Controllers.Admin
{
    /// <summary>
    /// 定时任务
    /// </summary>
    public class SysJobController : WebController
    {
        private readonly IQuartzModuleCollection _moduleCollection;
        private readonly ISysJobService _service;
        public SysJobController(ISysJobService sysJobService,IQuartzModuleCollection moduleCollection)
        {
            _moduleCollection = moduleCollection;
            _service = sysJobService;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Power("添加")]
        [AllowAnonymous]
        public async Task<IApiResult> Add([FromBody]JobAddModel model)
        {
            return await _service.Add(model);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Menu("任务列表")]
        [AllowAnonymous]
        [HttpGet]
        public async Task<IApiResult> List([FromQuery]JobQueryModel model)
        {
            return await _service.Query(model);
        }
        /// <summary>
        /// job下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Common]
        public IApiResult JobSelect()
        {
            var module = _moduleCollection.FirstOrDefault();
            if (module == null)
                return ApiResult.NotOk();

            return ApiResult.Ok(module.TaskSelect);
        }
        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Power("启动")]
        [AllowAnonymous]
        public Task<IApiResult> Resume([FromBody]string id)
        {
            return _service.Resume(id);
        }
        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Power("暂停")]
        [AllowAnonymous]
        public Task<IApiResult> Pause([FromBody]string id)
        {
            return _service.Pause(id);
        }
        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Power("停止")]
        [AllowAnonymous]
        public Task<IApiResult> Stop([FromBody]string id)
        {
            return _service.Stop(id);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [Power("删除")]
        [AllowAnonymous]
        public Task<IApiResult> Delete([FromBody]string ids)
        {
            return _service.Delete(ids);
        }

        /// <summary>
        /// 日志
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        [Power("查看日志")]
        [AllowAnonymous]
        public Task<IApiResult> Log([FromQuery]JobLogQueryModel  model)
        {
            return _service.Log(model);
        }




    }
}